
const PORT = 8000;
const { Pool } = require('pg');
const pool = new Pool({
    user: 'postgres',
    host: 'project_data_base',
    //host: '127.0.0.1',
    database: 'postgres',
    password: '1234',
    port: 5432,
  });

class feeding {
  /**
 * @swagger
 * components:
 *   schemas:
 *     Feeding:
 *       type: object
 *       properties:
 *         id:
 *           type: integer
 *           description: The auto-generated id of the feeding
 *         amount:
 *           type: number
 *           description: The amount of food
 *         feeding_time:
 *           type: string
 *           format: date-time
 *           description: The time of feeding
 *         food_brand:
 *           type: integer
 *           description: The ID of the food brand
 *         food_composition:
 *           type: string
 *           description: The composition of the food
 *         id_population:
 *           type: integer
 *           description: The ID of the population
 *       required:
 *         - amount
 *         - feeding_time
 *         - food_brand
 *         - food_composition
 *         - id_population
 *       example:
 *         amount: 500
 *         feeding_time: "2023-01-01T10:00:00Z"
 *         food_brand: 1
 *         food_composition: "Protein, Fat, Carbohydrates"
 *         id_population: 1
 *     FeedingResponse:
 *       type: object
 *       properties:
 *         feedings:
 *           type: array
 *           items:
 *             $ref: '#/components/schemas/Feeding'
 *         next:
 *           type: string
 *           description: The link to the next page of results
 *         previous:
 *           type: string
 *           description: The link to the previous page of results
 *       example:
 *         feedings:
 *           - id: 1
 *             amount: 500
 *             feeding_time: "2023-01-01T10:00:00Z"
 *             food_brand: 1
 *             food_composition: "Protein, Fat, Carbohydrates"
 *             id_population: 1
 *         next: "http://localhost:8000/feeding?limit=10&offset=10"
 *         previous: null
     * /feeding:
     *   get:
     *     summary: Получение списка кормлений
     *     tags: [Кормление]
     *     parameters:
     *       - in: query
     *         name: limit
     *         schema:
     *           type: integer
     *         description: Количество элементов списка
     *       - in: query
     *         name: offset
     *         schema:
     *           type: integer
     *         description: The number of items to skip before starting to collect the result set
     *     responses:
     *       200:
     *         description: Список кормлений
     *         content:
     *           application/json:
     *             schema:
     *               type: object
     *               properties:
     *                 feedings:
     *                   type: array
     *                   items:
     *                     type: object
     *                 next:
     *                   type: string
     *                 previous:
     *                   type: string
     *       500:
     *         description: Внутрення ошибка сервера
     */
    async feedingGet (req, res) {
          try {
            const limit = req.query.limit || 10;
            const offset = req.query.offset || 0;
        
            const queryText = `
            SELECT * FROM food_populations fp join population on fp.id_population = population.pid`;
        
            const result = await pool.query(queryText);
            const feedings = result.rows;
            console.log(feedings)
            const nextOffset = feedings.length < limit ? 0 : parseInt(offset) + parseInt(limit);
            const previousOffset = parseInt(offset) - parseInt(limit) >= 0 ? parseInt(offset) - parseInt(limit) : null;
        
            const response = {
              feedings,
              next: nextOffset === 0 ? null : `http://localhost:${PORT}/feeding?limit=${limit}&offset=${nextOffset}`,
              previous: previousOffset === null? null : `http://localhost:${PORT}/feeding?limit=${limit}&offset=${previousOffset}`,
            };
        
            res.json(response);
          } catch (error) {
            res.status(500).json({ error: error.message });
          }
        }
     /**
     * @swagger
     * /feeding:
     *   post:
     *     summary: Новое кормление создано
     *     tags: [Кормление]
     *     requestBody:
     *       required: true
     *       content:
     *         application/json:
     *           schema:
     *             type: object
     *             properties:
     *               amount:
     *                 type: number
     *               feeding_time:
     *                 type: string
     *                 format: date-time
     *               food_brand:
     *                 type: integer
     *               food_composition:
     *                 type: string
     *               id_population:
     *                 type: integer
     *     responses:
     *       200:
     *         description: The created feeding
     *         content:
     *           application/json:
     *             schema:
     *               type: object
     *       500:
     *         description: Ошибка сервера
     */   
    async feedingPost (req, res){
          try {
            const { amount, food_brand, food_size, food_batch, id_population, feeding_time, } = req.body;
        
            const queryText = `
              INSERT INTO food_populations (amount, food_brand, food_size, food_batch, id_population, feeding_time)
              VALUES ($1, $2, $3, $4, $5, $6)
              RETURNING *;
            `;
            const values = [amount,  food_brand, food_size, food_batch, id_population, feeding_time];
            console.log(values)
            const result = await pool.query(queryText, values);
            const newFeeding = result.rows[0];
        
            res.json(newFeeding);
          } catch (error) {
            res.status(500).json({ error: error.message });
          }
        }
}

module.exports = new feeding()